﻿using LCL;
using LCL.CodeHelper;
using LCL.IBuilder;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace LCL.Builder.IBatisNet
{
    public partial class ProjectBuilder
    {
        private void BuilderMaps()
        {
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                var Fieldlist = DbObject.GetColumnInfoList(DbName, table.TableName);
                var PKey = Fieldlist.FirstOrDefault<TableColumn>(p => p.IsPrimaryKey == true);
                string _key = "ID";//标识列，或主键字段		
                string _keyType = "int";//标识列，或主键字段类型   
                if (PKey != null)
                {
                    _key = PKey.ColumnName;
                    _keyType = CodeCommon.DbTypeToCS(PKey.ColumnType);
                }
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                StringBuilder property = new StringBuilder();
                StringBuilder parameter = new StringBuilder();
                StringBuilder select = new StringBuilder();
                StringBuilder delete = new StringBuilder();
                StringBuilder update = new StringBuilder();
                StringBuilder insert = new StringBuilder();
                foreach (var field in Fieldlist)
                {
                    string columnName = field.ColumnName;
                    string columnType = field.ColumnType;
                    bool IsIdentity = field.IsIdentity;
                    bool ispk = field.IsPrimaryKey;
                    bool cisnull = field.IsNullable;
                    string deText = field.ColumnRemark;
                    //columnType = CodeCommon.DbTypeToCS(columnType);
                    string length = field.MaxLength.ToString();
                    property.AppendLine("<result property=\"" + columnName + "\" nullValue=\"0\" column=\"" + columnName + "\" /> ");
                    parameter.AppendLine("<parameter property=\"" + columnName + "\" column=\"" + columnName + "\"   dbType=\"" + columnType + "\"  size=\"" + length + "\" /> ");
                    select.AppendLine("<isNotNull prepend=\"AND\" property=\"" + columnName + "\">" + columnName + "=#" + columnName + "#</isNotNull> ");
                    delete.AppendLine("      " + columnName + "=#" + columnName + "#, ");
                    update.AppendLine("      " + columnName + "=?, ");
                    insert.Append("?,");
                }
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> ");
                builder.AppendLine("<sqlMap namespace=\"" + className + "\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://ibatis.apache.org/mapping\"> ");
                builder.AppendLine("	<alias> ");
                builder.AppendLine("		<typeAlias alias=\"" + className + "\"  type=\"" + NameSpace + "." + className + "," + NameSpace + ".Model\" /> ");
                builder.AppendLine("	</alias> ");
                builder.AppendLine("  <resultMaps> ");
                builder.AppendLine("    <resultMap id=\"" + className + "_AllColumns\" class=\"" + className + "\"> ");
                builder.AppendLine(property.ToString());
                builder.AppendLine("    </resultMap> ");
                builder.AppendLine("  </resultMaps> ");
                builder.AppendLine("  <parameterMaps> ");
                builder.AppendLine("    <parameterMap id=\"" + className + "_AllParams\" class=\"" + className + "\"> ");
                builder.AppendLine(parameter.ToString());
                builder.AppendLine("    </parameterMap> ");
                builder.AppendLine("  </parameterMaps> ");
                builder.AppendLine("  <statements> ");
                builder.AppendLine("    <select id=\"" + className + "_Select\" resultMap=\"" + className + "_AllColumns\" parameterClass=\"Hashtable\"> ");
                builder.AppendLine("      select " + CodeCommon.GetSeleteColumn(Fieldlist, "", false) + " ");
                builder.AppendLine("      from " + table.TableName + " ");
                builder.AppendLine("      <dynamic prepend=\"where\"> ");
                builder.AppendLine(select.ToString());
                builder.AppendLine("      </dynamic> ");
                builder.AppendLine("    </select> ");
                builder.AppendLine("    <delete id=\"" + className + "_Delete\" parameterClass=\"" + className + "\"> ");
                builder.AppendLine("      delete from " + table.TableName + " ");
                builder.AppendLine("      where ");
                builder.AppendLine(delete.DelLastComma());
                builder.AppendLine("    </delete> ");
                builder.AppendLine("    <update id=\"" + className + "_Update\" parameterMap=\"" + className + "_AllParams\" resultClass=\"" + _keyType + "\"> ");
                builder.AppendLine("      update " + table.TableName + " set ");
                builder.AppendLine(update.DelLastComma());
                builder.AppendLine("      where ");
                builder.AppendLine("      " + _key + "=? ");
                builder.AppendLine("    </update> ");
                builder.AppendLine("    <insert id=\"" + className + "_Insert\"  parameterMap=\"" + className + "_AllParams\" resultClass=\"" + _keyType + "\"> ");
                builder.AppendLine("      <selectKey property=\"" + _key + "\" type=\"pre\" resultClass=\"" + _keyType + "\"> ");
                builder.AppendLine("        select " + table.TableName + "_S.nextval as value from dual ");
                builder.AppendLine("      </selectKey> ");
                builder.AppendLine("      insert into " + table.TableName + " ");
                builder.AppendLine("      (" + CodeCommon.GetSeleteColumn(Fieldlist, "", false) + ") ");
                builder.AppendLine("      values ");
                builder.AppendLine("      (" + insert.DelLastComma() + ") ");
                builder.AppendLine("    </insert> ");
                builder.AppendLine("  </statements> ");
                builder.AppendLine("</sqlMap> ");

                string filename = CodeCommon.DesktopDirectory + NameSpace + @"/WebAPI/Maps/" + className + ".xml";
                CodeCommon.CreateFiles(filename,FormatXml(builder.ToString()));
            }
        }
       
    }
}
